package me.xuym.crawler.dygang;

import com.cbs.java.component.application.Application;
import com.cbs.java.component.application.BaseArgs;
import me.xuym.crawler.Task;
import me.xuym.crawler.dygang.convertor.DYGangWebConvertor;
import me.xuym.crawler.dygang.loader.DYGangDBLoader;
import me.xuym.crawler.dygang.loader.DYGangWebDetailLoader;
import me.xuym.crawler.dygang.loader.DoubanLoader;
import me.xuym.crawler.simpleextension.dispatcher.SimpleDebugListDispatcher;
import me.xuym.crawler.simpleextension.filter.SimpleCachedMySQLListFilter;
import me.xuym.crawler.simpleextension.loader.SimpleWebPageLoader;
import me.xuym.crawler.simpleextension.processor.SimpleResourceUpdateMySQLListProcessor;

/**
 * Created by broche on 08/09/2017.
 */
public class MyApplication extends Application {
    private BaseArgs baseArgs = new BaseArgs();

    public MyApplication(String[] args) {
        super(args);
    }

    @Override
    public boolean isDebug() {
        return false;
    }

    @Override
    public String applicationName() {
        return "crawler/dygang";
    }

    @Override
    public BaseArgs getArgs() {
        return baseArgs;
    }

    @Override
    public void start() {
        super.start();
        try {
            String DYGangURL = "http://www.dygang.net/ys/index.htm";
            String DBHost = getConfigs().getProperty("DBHost", "");
            String DBPort = getConfigs().getProperty("DBPort", "");
            String DBUsername = getConfigs().getProperty("DBUsername", "");
            String DBPassword = getConfigs().getProperty("DBPassword", "");
            String DBDatabase = getConfigs().getProperty("DBDatabase", "");

            Task task = new Task(this);
            // 新片
            task.process(new SimpleWebPageLoader(this, DYGangURL, "gbk"))
                    .process(new DYGangWebConvertor(this))
                    .process(new SimpleCachedMySQLListFilter(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase, "dygang").setDebug(false))
                    .process(new DYGangDBLoader(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase))
                    .process(new DoubanLoader(this))
                    .process(new SimpleDebugListDispatcher(this))
                    .process(new SimpleResourceUpdateMySQLListProcessor(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase, "dygang"))
                    .process(new DYGangWebDetailLoader(this))
                    .process(new SimpleCachedMySQLListFilter(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase, "dygangdownload").setDebug(false));


            // 老片
            task.process(new DYGangDBLoader(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase))
                    .process(new DoubanLoader(this))
                    .process(new SimpleResourceUpdateMySQLListProcessor(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase, "dygang"))
                    .process(new DYGangWebDetailLoader(this))
                    .process(new SimpleCachedMySQLListFilter(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase, "dygangdownload").setDebug(false));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyApplication myApplication = new MyApplication(args);
        myApplication.start();
    }
}
